var core = require('core');
var send = core.send;
var wechat = core.wechat;
var step = core.step;
var qr = core.util.qrcode;

var order = function(req, res, next){
    var body = req.body;
    var template = "open/order";
    var c, Merchant, SEND;

    var getOrder = function(){
        var that = this;
        core.api("order/get", body, function(err, doc){
            that.step({err: err, doc: doc});
        });
    };

    var getOrderEnd = function(result){
        var err = result.err;
        var doc = result.doc;
        var that = this;
        if(err){
            template = "admin/error";
            SEND = new send(200000);
            return that.end();
        };
        if(doc.code){
            template = "admin/error";
            SEND = doc;
            return that.end();
        }
        Order = doc.docs;

        return true;
    };

    var getMerchat = function(result){
        var that = this;
        var params = {
            mid : Order.mid
        };
        core.api("merchant/find", params, function(err, doc){
            that.step({err: err, doc: doc});
        });
    };

    var getMerchatEnd = function(result){
        var err = result.err;
        var doc = result.doc;
        var that = this;
        if(err){
            template = "admin/error";
            SEND = new send(200000);
            that.end();
        };
        if(doc.code){
            template = "admin/error";
            SEND = doc;
            return that.end();
        }
        Merchant = doc.docs;

        return true;
    };

    var getRegion = function(){
        var that = this;
        var params = {
            p : Merchant.address.area[0],
            c : Merchant.address.area[1],
            a : Merchant.address.area[2]
        };
        core.api("open/getRegion", params, function(err, doc){
            that.step({err: err, doc: doc});
        });
    };

    var getRegionEnd = function(result){
        var err = result.err;
        var doc = result.doc;
        var that = this;
        if(err){
            template = "admin/error";
            SEND = new send(200000);
            that.end();
        };
        if(doc.code){
            template = "admin/error";
            SEND = doc;
            return that.end();
        };

        Merchant.address.area[0] = doc.docs[0].label;
        Merchant.address.area[1] = doc.docs[0].children[0].label;
        Merchant.address.area[2] = doc.docs[0].children[0].children[0].label;

        return true;
    }

    var qrcode = function(){
        var url = wechat.config.URL + "open/order_confirm?oid="+ Order.oid +"&mid="+ Order.mid;
        var opt = {type: 'png'};
        var img = qr(url, opt);
        return img;
    };

    var end = function(result){
        if(!Order.status){
            template = "admin/error";
            SEND = new send(300004);
        };
        if(!SEND){
            SEND = new send(0);
            SEND.docs = {
                money : Order.money / 100,
                title : Order.body,
                name : Merchant.name,
                address : Merchant.address.area.join("/")+"&nbsp;"+Merchant.address.text,
                qrcode : result
            };
            SEND.docs.money = SEND.docs.money.toFixed(2);
        };

        SEND.docs = SEND.docs || {};
        SEND.docs.jssdk = wechat.getJSSDK(req.originalUrl)

        res.render(template, SEND);
    };

    step.Step(getOrder, getOrderEnd, getMerchat, getMerchatEnd, getRegion, getRegionEnd, qrcode, end);
};

module.exports = order;
